package com.boolib.animate.commons.base;



import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * @author nier
 * @create 2020-02-19 19:22
 */

public class BaseController <T extends BaseEntity>{

    @Resource
    protected HttpServletRequest request;

    @Resource
    protected  HttpServletResponse response;

    @Resource
    protected HttpSession httpSession;

    private T getT(){
        Class<T> clazz= (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        throw new RuntimeException();
    }


    private Class<T> getTClass(){
     return (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    @Autowired
    protected BaseCurdService<T> baseCurdService;



    @PostMapping
    public T save(@RequestBody T t){
        return baseCurdService.save(t);
    }


    @PostMapping("list")
    public Iterable<T> save(@RequestBody List<T> t){
        return baseCurdService.saveBatch(t);
    }


    @ResponseStatus(HttpStatus.NO_CONTENT)
    @DeleteMapping
    public void delete(@RequestBody T t){
        baseCurdService.delete(t);
    }


    @ResponseStatus(HttpStatus.NO_CONTENT)
    @DeleteMapping("{id}")
    public void delete(@PathVariable("id") Long id){
        baseCurdService.deleteSoftById(id);
    }


    @PatchMapping("{id}")
    public T updatePath(@PathVariable("id") Long id,@RequestBody T t){

        return baseCurdService.updateByPrimaryKeySelective(id,t);
    }



    @PutMapping("{id}")
    public T delete(@PathVariable("id") Long id,@RequestBody T t){
        return   baseCurdService.updateById(id,t);
    }



    @GetMapping
    public Object get(

            @RequestParam(value = "page",required = false) Integer page,
                           @RequestParam(value = "size",required = false) Integer size,
                           @RequestParam(value = "sort",required = false) String sort,
                           @RequestParam(value = "sortType",required = false) String  sortType
                           ){

            final Class<T> tClass = getTClass();

            Example example = new Example( tClass);



            if(!Strings.isBlank(sort)){

                final Example.OrderBy orderBy = example.orderBy(sort);
                if("desc".equals(sortType)){
                    orderBy.desc();
                }else{
                    orderBy.asc();
                }

                if(page!=null &&size !=null){
                    return     baseCurdService.queryByExampleAndRowBounds(example,page,size);
                }

                return baseCurdService.queryByExample(example);

            }else if(page!=null&&size!=null){
                return  baseCurdService.queryByExampleAndRowBounds(example,page,size);
            }

            return baseCurdService.queryByExample(example);


    }


//
//    @GetMapping("{id}")
//    public T query(@PathVariable("id") Long id){
//        return baseCurdService.queryById(id);
//    }






}
